package com.baidu.health.service.impl;

import com.baidu.health.mapper.TbAilmentFatorMapper;
import com.baidu.health.mapper.TbAilmentMapper;
import com.baidu.health.pojo.ResultResponse;
import com.baidu.health.pojo.TbAilment;
import com.baidu.health.pojo.TbAilmentFator;
import com.baidu.health.service.TbAilmentFatorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbAilmentFatorServiceImpl extends ServiceImpl<TbAilmentFatorMapper, TbAilmentFator>
    implements TbAilmentFatorService{
    @Autowired
    private TbAilmentFatorMapper tbAilmentFatorMapper;
    @Autowired
    private TbAilmentMapper tbAilmentMapper;
    @Override
    public ResultResponse ailmentFactorSelect(TbAilmentFator tbAilmentFator) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("factor_id",tbAilmentFator.getFactorId());
        List<TbAilmentFator> list = tbAilmentFatorMapper.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            Integer ailmentId = list.get(i).getAilmentId();
            TbAilment tbAilment = tbAilmentMapper.selectById(ailmentId);
            list.get(i).setAilmentName(tbAilment.getAilmentName());
        }
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse ailmentFactorInsert(TbAilmentFator tbAilmentFator) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ailment_id",tbAilmentFator.getAilmentId());
        queryWrapper.eq("factor_id",tbAilmentFator.getFactorId());
        TbAilmentFator tbAilmentFator1 = tbAilmentFatorMapper.selectOne(queryWrapper);
        if(tbAilmentFator1!=null){
            return ResultResponse.FAILED(500,"相同类型的智能进行一次添加");
        }
        tbAilmentFator.setCreateTime(new Date());
        tbAilmentFatorMapper.insert(tbAilmentFator);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse ailmentFactorUpdate(TbAilmentFator tbAilmentFator) {
        tbAilmentFatorMapper.updateById(tbAilmentFator);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse ailmentFactorDeleted(Integer factorId) {
        tbAilmentFatorMapper.deleteById(factorId);
        return ResultResponse.SUCCESS();
    }
}




